package jwt

import (
	"github.com/dgrijalva/jwt-go"
	"github.com/syyongx/php2go"
	"time"
)

var SecretKey string

func init()  {
	SecretKey=php2go.Uniqid("")
}

//制作Token
func MakeToken()  string{


	token := jwt.New(jwt.SigningMethodHS256)
	claims := make(jwt.MapClaims)
	claims["exp"] = time.Now().Add(time.Hour * time.Duration(1)).Unix() //一个小时
	claims["iat"] = time.Now().Unix()
	token.Claims = claims

	tokenString, err := token.SignedString([]byte(SecretKey))
	if err != nil {

	}
	return tokenString
}

//验证Token
func ValidateToken(token string) bool  {
	
	//token, err := request.ParseFromRequest(r, request.AuthorizationHeaderExtractor,
	//	func(token *jwt.Token) (interface{}, error) {
	//		return []byte(SecretKey), nil
	//	})

	//if err == nil {
	//	if token.Valid {
	//		next(w, r)
	//	} else {
	//		w.WriteHeader(http.StatusUnauthorized)
	//		fmt.Fprint(w, "Token is not valid")
	//	}
	//} else {
	//	w.WriteHeader(http.StatusUnauthorized)
	//	fmt.Fprint(w, "Unauthorized access to this resource")
	//}

	return true
}